package com.huaxin.common.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.huaxin.common.Entity.SmallDeviceInfo;
import com.huaxin.common.R;

import java.util.List;

/**
 * @author lwjing
 * @date 2018/6/14
 * @Desc 子设备信息弹框
 * Copyright HUAXIN DATA. All rights reserved.
 */
@SuppressLint("ValidFragment")
public class BottomSheetDialogOther extends DialogFragment implements View.OnClickListener {
    private       Activity              mContext;
    private final LayoutInflater        mInflater;
    private       RecyclerView.Adapter  mAdapter;
    private       List<SmallDeviceInfo> mDatas;
    private       TextView              mTilte;
    private       Button                mCancel;


    public BottomSheetDialogOther(Activity context, List<SmallDeviceInfo> deviceInfos) {
        mContext = context;
        mDatas = deviceInfos;
        mInflater = LayoutInflater.from(mContext);
        mAdapter = new MyAdapter();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        View rootView = View.inflate(mContext, R.layout.dialog_bottom_sheet_other, null);
        Dialog dialog = new Dialog(mContext, R.style.Translucent_NoTitle);
        dialog.setContentView(rootView);
        dialog.setCanceledOnTouchOutside(true);
        Window alertWindow = dialog.getWindow();
        alertWindow.setGravity(Gravity.BOTTOM);
        RecyclerView listview = (RecyclerView) rootView.findViewById(R.id.listView);
        listview.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        listview.setAdapter(mAdapter);
        mCancel = (Button) rootView.findViewById(R.id.btn_cancel);
        mTilte = (TextView) rootView.findViewById(R.id.dialog_title);
        mCancel.setOnClickListener(this);

        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        //dialog 高度适配
        if (mDatas != null && mDatas.size() <= 5) {
            dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        } else {
            //dialog最高高度不超过屏幕的3/4
            dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, dm.heightPixels*3/4);
        }
        return dialog;
    }

    public void show(FragmentManager manager, String tag) {
        FragmentTransaction ft = manager.beginTransaction();
        ft.add(this, tag);
        ft.commitAllowingStateLoss();
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_cancel) {
            dismiss();
        }
    }

    protected class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyViewHolder(mInflater.inflate(R.layout.item_bottom_dialog, parent, false));
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            holder.update(position);
        }


        @Override
        public int getItemCount() {
            return mDatas == null ? 0 : mDatas.size();
        }


        public class MyViewHolder extends RecyclerView.ViewHolder {
            View     itemView;
            TextView info_name;
            TextView info_content;
            int      pos;

            public MyViewHolder(View itemView) {
                super(itemView);
                this.itemView = itemView;
                info_name = (TextView) itemView.findViewById(R.id.info_name);
                info_content = (TextView) itemView.findViewById(R.id.info_content);
            }

            public void update(int position) {
                this.pos = position;
                info_name.setText(mDatas.get(position).getTitle());
                info_content.setText(mDatas.get(position).getValue());
            }
        }

    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

}
